% this is the script for plotting MRA data
% Version: test
clc;
clear all;
close all;

%Read data file
    %fileName=input('Input a file name: ','s');
    fileName='test_grain_list.txt';
    f_id=fopen(fileName);
    label=textscan(f_id,'%s %s %s %s %s %s %s %s',1);
    data=fscanf(f_id,'%f',[8 inf]);
    f_return=fclose(f_id);
    
    id=data(1,:);
    area=data(2,:);
    density=data(3,:);
    sizeDens=data(4,:);
    clusterDens=data(5,:);
    freq=data(6,:);
    de=data(7,:);
    nFreq=data(8,:);
    
    diameter=sqrt(area/pi)*2;
    minDiameter=min(diameter);
    logDiameter=log10(diameter/minDiameter);
    
    maxLogDiameter=max(logDiameter);
    sizeInt=maxLogDiameter/3;
    selected=find(logDiameter>=sizeInt*2 & logDiameter<sizeInt*3);

    x=(nFreq(selected))';
    y=(density(selected))';
    z=(logDiameter(selected))';
    
    polyfit(x,y,1)
    
    %xy=[x;y]';
    
    intv=0:0.005:1;
    [coX coY]=meshgrid(intv,intv);
    
    for i=1:size(intv,2)
        for j=1:size(intv,2)
            coZ(i,j)=size(find(power(x-coX(i,j),2)+power(y-coY(i,j),2)<power(0.01,2)),1);
            coZ(i,j)=coZ(i,j)/size(id,2);
        end;
    end;

    
    %intv=0.1:0.001:0.7;
    intv=min(min(x), min(y))-0.1:0.001:max(max(x), max(y)+0.1);
    [XI,YI] = meshgrid(intv, intv);
    ZI = griddata(coX,coY,coZ,XI,YI);
    %hold on;
    contour(XI,YI,ZI); 
    
    
%plot data
    %hold on;
    grid on;
    %scatter3(x,y,z,'filled');
    %scatter3(x,y,z,diameter*5,nFreq,'filled');
    %scatter(x,y,diameter(selected)*3,diameter(selected),'filled');
    %scatter(nFreq,density);
    %scatter(nFreq,logDiameter,area);

    %hold off;    

    %colormap jet;
    
    %axis_font_sizeDensDens=10;
    axis equal;
    %axis ([0 1 0 1]);
    %axis ([min(x) max(x) min(y) max(y)]);
    %xlabel('Normalized Frequency','fontsizeDensDens',axis_font_sizeDensDens);
    %ylabel('Area Density','fontsizeDensDens',axis_font_sizeDensDens);
    %zlabel('Normalized Diameter (log)','fontsizeDensDens',axis_font_sizeDensDens);
    %grid off;
    %camlight left;
